package com.lanou3g.dllo.baidumusic.login;

import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.a.a.I;
import com.lanou3g.dllo.baidumusic.R;
import com.lanou3g.dllo.baidumusic.base.BaseActivity;
import com.lanou3g.dllo.baidumusic.base.MyApp;
import com.lanou3g.dllo.baidumusic.database.MyFavorite;
import com.lanou3g.dllo.baidumusic.database.SingleLiteOrm;
import com.lanou3g.dllo.baidumusic.main.MainActivity;
import com.lanou3g.dllo.baidumusic.mine.myfavorite.MyFavoriteManager;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by dllo on 16/7/6.
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {

    private EditText loginUserName, loginPassword;
    private TextView userRegister;
    private Button loginBtn;
    private ImageView loginReturn;
    private CheckBox loginCheckBox;
    private MyFavorite myFavorite;

    @Override
    public int setLayout() {
        return R.layout.activity_login;
    }

    @Override
    public void initView() {
        loginUserName = (EditText) findViewById(R.id.login_username);
        loginPassword = (EditText) findViewById(R.id.login_password);
        userRegister = (TextView) findViewById(R.id.user_register);
        loginBtn = (Button) findViewById(R.id.login_btn);
        loginReturn = (ImageView) findViewById(R.id.login_return);
        loginCheckBox = (CheckBox) findViewById(R.id.login_checkbox);

        userRegister.setOnClickListener(this);
        loginBtn.setOnClickListener(this);
        loginReturn.setOnClickListener(this);
        loginCheckBox.setOnClickListener(this);

    }

    @Override
    public void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_return:
                finish();
                break;
            case R.id.login_btn:
                String inputInfo = loginUserName.getText().toString();
                String inputinfo = loginPassword.getText().toString();
                if (inputInfo.isEmpty() || inputinfo.isEmpty()) {
                    Toast.makeText(this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                } else {
                    BmobUser bmobUser = new BmobUser();
                    bmobUser.setUsername(loginUserName.getText().toString());
                    bmobUser.setPassword(loginPassword.getText().toString());
                    bmobUser.login(this, new SaveListener() {
                        @Override
                        public void onSuccess() {
                            // 登录成功跳转到MainActivity
                            loginGetInternetData();
                            // 刷新我的界面我喜欢的单曲的歌曲数
                            Intent intent1 = new Intent(getPackageName() + ".REFRESHMYFAVORITE");
                            sendBroadcast(intent1);
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                break;

            // 去进行注册
            case R.id.user_register:
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
                break;
            case R.id.login_checkbox:
                if (loginCheckBox.isChecked()) {
                    loginPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    loginPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;

        }

    }

    // 登录后获取网络数据
    public void loginGetInternetData() {
        BmobUser bmobUser = BmobUser.getCurrentUser(this);
        if (bmobUser != null) {
            myFavorite = new MyFavorite();
            BmobQuery<MyFavorite> bmobQuery = new BmobQuery<>();
            bmobQuery.addWhereEqualTo("userName", bmobUser.getUsername());
            bmobQuery.findObjects(this, new FindListener<MyFavorite>() {
                @Override
                public void onSuccess(List<MyFavorite> list) {
                    MyFavoriteManager.getInstance().getMyFavorites().clear();
                    for (MyFavorite myFavorite : list) {
                        MyFavoriteManager.getInstance().getMyFavorites().add(myFavorite);
                    }
                    int number = 0;
                    for (MyFavorite song : SingleLiteOrm.getSingleLiteOrm().getLiteOrm().query(MyFavorite.class)) {
                        for (MyFavorite favoriteSong : MyFavoriteManager.getInstance().getMyFavorites()) {
                            if (song.getTitle().equals(favoriteSong.getTitle())) {
                                number++;
                            }
                        }
                        if (number == 0) {
                            song.setUserName(BmobUser.getCurrentUser(MyApp.context).getUsername());
                            song.save(MyApp.context, new SaveListener() {
                                @Override
                                public void onSuccess() {
                                    getNetData();
                                }

                                @Override
                                public void onFailure(int i, String s) {

                                }
                            });
                        } else {
                            number = 0;
                        }
                    }

                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }
    }

    // 获取网络数据
    public void getNetData() {
        if (BmobUser.getCurrentUser(MyApp.context) != null) {
            BmobQuery<MyFavorite> bmobQuery = new BmobQuery<>();
            bmobQuery.addWhereEqualTo("userName", BmobUser.getCurrentUser(MyApp.context).getUsername());
            bmobQuery.findObjects(this, new FindListener<MyFavorite>() {
                @Override
                public void onSuccess(List<MyFavorite> list) {
                    MyFavoriteManager.getInstance().getMyFavorites().clear();
                    for (MyFavorite myFavorite : list) {
                        MyFavoriteManager.getInstance().getMyFavorites().add(myFavorite);
                    }
                    // 刷新我的界面我喜欢的单曲的歌曲数
                    Intent intent = new Intent(getPackageName() + ".REFRESHMYFAVORITE");
                    sendBroadcast(intent);
                }

                @Override
                public void onError(int i, String s) {

                }
            });
        }
    }

}
